package cn.bsuc.crm.controller;


import cn.bsuc.crm.dto.ModuleDTO;
import cn.bsuc.crm.entity.Module;
import cn.bsuc.crm.service.ModuleService;
import cn.bsuc.crm.util.Code;
import cn.bsuc.crm.util.MenuTreeModel;
import cn.bsuc.crm.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhuoyuan
 * @since 2022-10-03
 */
@RestController
@RequestMapping("/module")
@CrossOrigin
public class ModuleController {

    @Autowired
    private ModuleService moduleService;
    //角色授权
    @GetMapping("/roleGetPermission")
    public Result roleGetPermission(Integer roleId){
        List<MenuTreeModel> allMenuTree = moduleService.getAllMenuTree(roleId);
        if (allMenuTree.size()!=0){
            return Result.ok(allMenuTree);
        }else if (allMenuTree.size()!=0){
            return  Result.fail(Code.ERROR,"查询失败");
        }else {
            return Result.fail(Code.SYSTEM_ERR,"系统出错");
        }
    }

    //角色的模块id
    @GetMapping("/seleteId")
    public Result seleteId(Integer roleId){
        List<Integer> list = moduleService.seleteId(roleId);
        if (list.size()!=0){
            return Result.ok(list);
        }else if (list.size()!=0){
            return  Result.fail(Code.ERROR,"查询失败");
        }else {
            return Result.fail(Code.SYSTEM_ERR,"系统出错");
        }
    }

    //保存菜单
    @PostMapping("/saveModule")
    public Result saveModule(@RequestBody ModuleDTO moduleDTO){
        int i = moduleService.saveMoule(moduleDTO,moduleDTO.getPid());
        if (i==1){
            return Result.ok();
        }else if (i==0){
            return  Result.fail(Code.ERROR,"保存失败");
        }else {
            return Result.fail(Code.SYSTEM_ERR,"系统出错");
        }
    }

    //id查信息
    @GetMapping("/selectModule")
    public Result selectModule(Integer id){
        Module byId = moduleService.getById(id);
        if (byId!=null){
            return Result.ok(byId);
        }else if (byId==null){
            return  Result.fail(Code.ERROR,"查询失败");
        }else {
            return Result.fail(Code.SYSTEM_ERR,"系统出错");
        }
    }

    //id删除信息
    @GetMapping("/deleteModule")
    public Result deleteModule(Integer id){
        int i = moduleService.deleteModule(id);
            return Result.ok();

    }
}

